package demo_thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author: android 超级兵
 * @create: 2022-01-15 13:06
 * TODO 线程的基本使用
 **/
public class Client1 {

    public static class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            System.out.println("方式一");
        }
    }


    public static class MyRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("方式二");
        }
    }

    public static class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            return "方式三";
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 方式一
        MyThread myThread = new MyThread();
        myThread.start();

        // 方式二
        MyRunnable myRunnable = new MyRunnable();
        new Thread(myRunnable).start();

        // 方式三
        MyCallable myCallable = new MyCallable();
        FutureTask<String> futureTask = new FutureTask<String>(myCallable) {
            @Override
            protected void done() {
                System.out.println("方式三开始前!");
                super.done();
                System.out.println("方式三执行后!");
                try {
                    System.out.println("done内部获取方式三返回结果:" + get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        };
        // 方式三启动线程
        new Thread(futureTask).start();

        System.out.println("获取方式三返回结果:" + futureTask.get());


        /*
         * @author: android 超级兵
         * @create: 2022/1/15 13:10
         * TODO 区别:
         *  方式三: 可以获取到返回值
         *  方式一: 需要手动调用run方法
         */
    }
}
